<?php
function displayTestPage(&$smarty, &$_POST) {
	//	取消数据库题库逻辑
	//	require_once '../util/dbconnect.php';
	//	$sql = sprintf ( "SELECT * FROM `scales` WHERE `id` = 1" );
	//	$result = $db->query ( $sql );
	//	$result = $result->fetch ();
	//	$content_json = $result ["content_json"];
	//	$content = json_decode ( $content_json );
	//	$smarty->assign ( "title", $result ["title"] );
	//	$smarty->assign ( "content", $content );
	$smarty->assign ( "arrLocation", array ("量表测试", "家庭亲密度问卷和适应性量表" ) );
	if (!isset($_POST['pageno'])) {
		// 量表介绍
		$pageno = 0;
		$smarty->assign("pageno", $pageno);
		$smarty->display ( "test_template/t1_intro.tpl" );
	} else {
		$pageno = $_POST['pageno'];
		$smarty->assign ( "pageno", $pageno );
		$strpercent = sprintf( "%01.2f%%", ($pageno-1)*100.0/6.0);
		$smarty->assign("strpercent", $strpercent);
		if ($pageno == 1) {
			// Fist Page need not SESSION and $_POST data
			$smarty->display ( "test_template/t1_content.tpl" );
		}elseif($pageno<=7){
			// other testing pages and redy page
			// 答题已完成页面同样要验证并保存$_POST数据到$_SESSION
			if(!isset($_SESSION['t1'])) $_SESSION['t1'] = array();
			if (count($_POST)<10+2) {
				$smarty->assign ( "msgTitle", "答题不完整" );
				$smarty->assign ( "message", "答题不完整,返回后重新回答!" );
				$smarty->display ( "../../info_back.tpl" );
				return;
			}
			// 完整10题
			$_SESSION['t1'][$pageno-1] = $_POST;
			$smarty->assign('debug_session', $_SESSION);
			if($pageno<=6) {
				$smarty->display ( "test_template/t1_content.tpl" );
			} elseif ($pageno==7){
				// 答题已完成页面
				$smarty->display ( "test_template/t1_ready.tpl" );
			}
		}else{
			die('pageno error.');
		}
	}
}
function displayTestResult(&$smarty, &$_POST) {
	
	$smarty->assign('debug_session', $_SESSION);
	if (count ( $_SESSION['t1'] ) < 6) {
		$smarty->assign ( "msgTitle", "答题不完整" );
		$smarty->assign ( "message", "答题不完整,返回后重新回答!" );
		$smarty->assign ( "location", 'doTest.php?t=1' );
		$smarty->display ( "../../info_redirect.tpl" );
		return;
	}
	$smarty->assign ( "arrLocation", array ("量表测试", "家庭亲密度问卷和适应性量表", "测试结果" ) );
	$v = array();
	for($i=1; $i<=6; ++$i){
		$v += $_SESSION['t1'][$i];
	}
	//$smarty->assign('debug_v', $v);
	// 亲密度得分=36+I—1+I—5+I—7+I—11+I—13+I—15+I—17+I—21+I—23+I—25+I—27+I—30-I—3-I—9-I—19-I—29
	$intimacy [0] = 36 + $v [0] + $v [4] + $v [6] + $v [10] + $v [13] + $v [14] + $v [16] + $v [20] + $v [22] + $v [24] + $v [26] + $v [29] - $v [2] - $v [8] - $v [19] - $v [28];
	// 适应性得分=12+I—2+I—4+I—6+I—8+I—10+I—12+I—14+I—16+I—18+I—20+I—22+I—26-I—24-I—28
	$fitness [0] = 12 + $v [1] + $v [3] + $v [5] + $v [7] + $v [9] + $v [11] + $v [13] + $v [15] + $v [17] + $v [19] + $v [21] + $v [25] - $v [23] - $v [27];
	
	$intimacy [1] = 36 + $v [30] + $v [34] + $v [36] + $v [40] + $v [43] + $v [44] + $v [46] + $v [50] + $v [52] + $v [54] + $v [56] + $v [59] - $v [32] - $v [38] - $v [49] - $v [58];
	$fitness [1] = 12 + $v [31] + $v [33] + $v [35] + $v [37] + $v [39] + $v [41] + $v [43] + $v [45] + $v [47] + $v [49] + $v [51] + $v [55] - $v [53] - $v [57];
	
	// print_r($intimacy);
	// print_r($fitness);
	$smarty->assign ( "i1", $intimacy [0] );
	$smarty->assign ( "i2", $intimacy [1] );
	$smarty->assign ( "f1", $fitness [0] );
	$smarty->assign ( "f2", $fitness [1] );
	
	$smarty->display ( "test_template/t1_result.tpl" );
}
